package com.hutao.manage.util;

import java.util.Date;



public class TongJiSaqlUtil {
	public static String disFeiyong(String yybm, com.hutao.manage.query.CybingrenTJSearch cybrSearch) {
		Date chuyuanrqStart = cybrSearch.getChuyuanrqStart();// 出院开始时间
		Date chuyuanrqEnd = cybrSearch.getChuyuanrqEnd();// 出院结束时间
		if (chuyuanrqStart == null || chuyuanrqEnd == null) {
			return null;
		}
		String cyks = cybrSearch.getCyks();// 出院科室
		String rybq = cybrSearch.getRybq();// 入院病情
		Integer startYear = cybrSearch.getStartYear();
		Integer startMonth = cybrSearch.getStartMonth();
		Integer startDay = cybrSearch.getStartDay();
		Integer endYear = cybrSearch.getEndYear();
		Integer endMonth = cybrSearch.getEndMonth();
		Integer endDay = cybrSearch.getEndDay();
		String lyfs = cybrSearch.getLyfs();// 离院方式
		String zhaiyao = cybrSearch.getZhaiyao();// 摘要
		Integer ageS=cybrSearch.getAgeS();
		Integer ageE=cybrSearch.getAgeE();
		StringBuilder rtSql = new StringBuilder(
				"select id from mr_in_baisc base ");
		rtSql.append(" where out_time ").append(
				StringUtil.getBeweenEndTimeByStartAndEndReal(chuyuanrqStart,
						chuyuanrqEnd));
		rtSql.append(" and organization_id='").append(yybm).append("' ");
		if (StringUtil.isKeYongStr(cyks)) {
			if(cyks.contains(",")){
				String [] cykses=cyks.split(",");
				rtSql.append(" and out_depa_id in (");
				
				for(int i=0;i<cykses.length;i++){
					rtSql.append("'").append(cykses[i]).append("'");
					if(i<cykses.length-1){
						rtSql.append(",");
					}
				}
				rtSql.append(") ");
			}else{
				rtSql.append(" and  out_depa_id='").append(cyks).append("' ");
			}
			
		}
		
		//年龄
		if(StringUtil.isKeYongStr(ageS)||StringUtil.isKeYongStr(ageE)){
			rtSql.append(" and ")
				.append(StringUtil.getAgeForSql(ageS,ageE));
		}
		/*if (StringUtil.isKeYongStr(startYear)
				&& StringUtil.isKeYongStr(endYear)) {
			rtSql.append(" and age_year between ").append(startYear)
					.append(" and ").append(endYear);
		}
		if (StringUtil.isKeYongStr(startMonth)
				&& StringUtil.isKeYongStr(endYear)) {
			rtSql.append(" and age_month between ").append(startYear)
					.append(" and ").append(endMonth);
		}
		if (StringUtil.isKeYongStr(startDay) && StringUtil.isKeYongStr(endDay)) {
			rtSql.append(" and age_day between ").append(startDay)
					.append(" and ").append(endDay);
		}*/
		if (StringUtil.isKeYongStr(lyfs)) {
			rtSql.append(" and ").append(
					StringUtil.getDouhaoFengeForSql("out_way", lyfs));
		}
		if (StringUtil.isKeYongStr(rybq)) {
			rtSql.append(" and ").append(
					StringUtil.getDouhaoFengeForSql("in_status", rybq));
		}

		if (StringUtil.isKeYongStr(zhaiyao)) {
			rtSql.append(" and ").append(
					StringUtil.getDouhaoFengeForSql_Like("chief_complaint", zhaiyao));
		}
		boolean jbFlag = cybrSearch.ishaveJB();
		boolean ssFlag = cybrSearch.ishaveSS();
		boolean zhongliuState = true;// 默认可设置
		if (jbFlag) {
			String jb_lei = cybrSearch.getJbzd_lei();
			String jb_ya = cybrSearch.getJbzd_ya();
			String jb_xi = cybrSearch.getJbzd_xi();
			rtSql.append(" and exists ( select serial_number from mr_in_diagnoss zd where zd.serial_number=base.id ");
			rtSql.append(" and zd.organization_id='").append(yybm).append("' ");
			rtSql.append("  and (");
			boolean jbzdFirstState = false;// 默认没有设置过，一旦设置过开头就要加or
			if (StringUtil.isKeYongStr(jb_lei)) {
				rtSql.append(StringUtil.getDouhaoFengeForSql_HouLike(
						"zd.diag_code", jb_lei));
				jbzdFirstState = true;
			}
			if (StringUtil.isKeYongStr(jb_ya)) {
				if (jbzdFirstState) {
					rtSql.append("  or ");
				}
				rtSql.append(
						StringUtil.getDouhaoFengeForSql_HouLike("zd.diag_code",
								jb_ya)).append("");
				jbzdFirstState = true;
			}
			if (StringUtil.isKeYongStr(jb_xi)) {
				if (jbzdFirstState) {
					rtSql.append("  or ");
				}
				rtSql.append(
						StringUtil.getDouhaoFengeForSql("zd.diag_code", jb_xi))
						.append("");
				jbzdFirstState = true;
			}
			String zl = cybrSearch.getZl();
			if (StringUtil.isKeYongStr(zl)) {
				if (jbzdFirstState) {
					rtSql.append("  or ");
				}
				rtSql.append(StringUtil.getDouhaoFengeForSql("zd.diag_code", zl));
				zhongliuState = false;
			}
			rtSql.append(" )");
			String zdlx = cybrSearch.getZdlx();
			if (StringUtil.isKeYongStr(zdlx)) {
				rtSql.append("  and (");
				boolean zdlxFirstState = false;// 默认没有设置过，一旦设置过开头就要加or
				if (zdlx.indexOf("0") != -1) {
					rtSql.append(" zd.diag_type='22' ");
					zdlxFirstState = true;
				}
				if (zdlx.indexOf("1") != -1) {
					if (zdlxFirstState) {
						rtSql.append(" or ");
					}
					rtSql.append(" zd.diag_order='1' ");
					zdlxFirstState = true;
				}
				if (zdlx.indexOf("2") != -1) {
					if (zdlxFirstState) {
						rtSql.append(" or ");
					}
					rtSql.append(" zd.diag_order='2' ");
					zdlxFirstState = true;
				}
				if (zdlx.indexOf("3") != -1) {
					if (zdlxFirstState) {
						rtSql.append(" or ");
					}
					rtSql.append(" zd.diag_order>2 ");
					zdlxFirstState = true;
				}
				rtSql.append("  ) ");
			}

			rtSql.append(" )");
		}

		// /肿瘤形态start
		String zl = cybrSearch.getZl();
		if (StringUtil.isKeYongStr(zl) && zhongliuState) {
			rtSql.append(" and exists ( select serial_number from mr_in_diagnoss zd where zd.serial_number=base.id ");
			rtSql.append(" and zd.organization_id='").append(yybm).append("' ");
			rtSql.append(" and ").append(
					StringUtil.getDouhaoFengeForSql("zd.diag_code", zl));

			rtSql.append(")");
		}
		// /肿瘤形态end
		if (ssFlag) {
			String ss_lei = cybrSearch.getSscz_lei();
			String ss_ya = cybrSearch.getSscz_ya();
			String ss_xi = cybrSearch.getSscz_xi();
			rtSql.append(" and exists ( select serial_number from mr_in_operation ss where ss.serial_number=base.id ");
			rtSql.append(" and ss.organization_id='").append(yybm).append("'  ");
			boolean FirstState = false;// 默认没有设置过，一旦设置过开头就要加or
			rtSql.append("  and (");
			if (StringUtil.isKeYongStr(ss_lei)) {
				rtSql.append(StringUtil.getDouhaoFengeForSql_HouLike(
						"ss.ops_code", ss_lei));
				FirstState = true;
			}
			if (StringUtil.isKeYongStr(ss_ya)) {
				if (FirstState) {
					rtSql.append("  or ");
				}
				rtSql.append(StringUtil.getDouhaoFengeForSql_HouLike(
						"ss.ops_code", ss_ya));
				FirstState = true;
			}
			if (StringUtil.isKeYongStr(ss_xi)) {
				if (FirstState) {
					rtSql.append("  or ");
				}
				rtSql.append(StringUtil
						.getDouhaoFengeForSql("ss.ops_code", ss_xi));
			}
			rtSql.append("  ) ");
			rtSql.append(" ) ");
		}
		// /责任人
		String zrr = cybrSearch.getZrr();
		if (StringUtil.isKeYongStr(zrr)) {
			rtSql.append(" and exists ( select serial_number from mr_in_doctor zrr where zrr.serial_number=base.id ");
			rtSql.append(" and zrr.organization_id='").append(yybm).append("'  ");
			rtSql.append(" and ").append(
					StringUtil.getDouhaoFengeForSql("zrr.doctor_id", zrr));
			if(StringUtil.isKeYongStr(cybrSearch.getMrliable())){
				rtSql.append(" and ").append(
						StringUtil.getDouhaoFengeForSql("zrr.doctor_type", cybrSearch.getMrliable()));
			}
			rtSql.append(")");
		}
		return rtSql.toString();
	}

	
}
